(function(window){
            // promise  构造函数
            function Promise(excutor){
                const self = this
                self.status = 'pending'
                self.data = undefined
                self.callbacks = [] //用来存储待执行的成功/失败的回调函数

                function resolve (value){
                    if(self.status != 'pending'){
                        return 
                    }

                    self.status = 'resolved'
                    self.data = value
                    if(self.callbacks.length > 0)
                    {
                        setTimeout(()=>{
                            self.callbacks.forEach(callbackObj => {
                                callbackObj.onResolved(value)
                            })
                        },0)
                    }
                }

                function reject(reason){
                    if(self.status != 'pending'){
                        return 
                    }

                    self.status = 'rejected'
                    self.data = reason
                    if(self.callbacks.length > 0)
                    {
                        setTimeout(()=>{
                            self.callbacks.forEach(callbackObj => {
                                callbackObj.onRejected(reason)
                            })
                        },0)
                    }
                }
               try {
                    excutor(resolve,reject)
               } catch (error) {
                   self.data = error
               }
            }
            // 用来指定成功和失败的回调方法
            Promise.prototype.then = function(onResolved, onRejected){
                const self = this
                // 如果不传入，系统帮传入的都是带有返回值的回调函数
                onResolved = typeof onResolved === 'function' ? onResolved : value=>value
                onRejected = typeof onRejected === 'function' ? onRejected : reason=>{throw reason}

                return new Promise((resolve,reject)=>{
                    // 统一处理下一次promise
                    function handle(callback){
                        try {
                            // 执行上次的的回调函数，并传给下一次promise的值
                            // 这个回调函数只有return才有值
                            const result = callback(self.data)
                            if(result instanceof Promise){
                                result.then(resolve,reject)
                            }else{
                                resolve(result)
                            }
                        } catch (error) {
                            reject(error)
                        }
                    }
                    if(self.status === 'resolved'){
                        setTimeout(()=>{
                            // try {
                            //     const result = onResolved(self.data)

                            //     if(result instanceof Promise){
                            //         // 更新promise状态值，我们都是公用的一个peomise
                            //         // 点then只为产生新的promise
                            //         // result.then(
                            //         //     value=>{
                            //         //         resolve(value)
                            //         //     },
                            //         //     reason=>{
                            //         //         reject(reason)
                            //         //     }
                            //         // )
                            //         // resolve默认接受value初始promise
                            //         // 用自己的resolve 和reject作为回调函数初始化promise
                            //         result.then(resolve,reject)
                            //     }else{
                            //         resolve(result)
                            //     }
                            // } catch (error) {
                            //     reject(error)
                            // }
                            handle(onResolved)
                        },0)
                    }
                    else if(self.status === 'rejected'){
                        handle(onRejected)
                    }
                    else{
                        this.callbacks.push({
                            onResolved: (value)=>{
                                handle(onResolved)
                            },
                            onRejected:(value)=>{
                                handle(onRejected)
                            }
                        })
                    }
                })
            }

            //失败的回调函数
            Promise.prototype.catch = function(onRejected){
                return this.then(null,onRejected)
            }

            //函数对象的静态方法
            // resolve 和reject都好像是立马产生了带有值的promise 通过自带的回调函数
            // 如果是promise 需要产生新的promise去覆盖，覆盖后刷新promise，相当于return了回调函数的值
            Promise.resolve = function(value){
                 return new Promise((resolve,reject)=>{
                    if(value instanceof Promise){
                        value.then(resolve, reject)
                    }else{
                        resolve(value)
                    }
                 })
            }
            // 延迟成功方法
            Promise.resolveDelay = function(value,time){
                return new Promise((resolve,reject)=>{
                    setTimeout(()=>{
                        if(value instanceof Promise){
                            value.then(resolve, reject)
                        }else{
                            resolve(value)
                        }
                    },time)
                })
           }


            Promise.reject = function(reason){
                return new Promise((resolve,reject)=>{
                    reject(reason)
                })
            }

            //延迟失败方法
            Promise.rejecDelay = function(reason,time){
                return new Promise((resolve,reject)=>{
                    setTimeout(()=>{
                        reject(reason)
                    },time)
                })
            }

            Promise.all = function(promises){
                return new Promise((resolve, reject)=>{

                    const length = promises.length
                    const values = new Array(length)
                    let resolvedCount = 0

                    promises.forEach( (p,index)=>{
                        Promise.resolve(p).then(
                            value =>{
                                resolvedCount++
                                values[index] = value
                                if(resolvedCount === length){
                                    resolve(values)
                                } 
                            },
                            reason =>{
                                reject(reason)
                            }
                        )
                    })
                })
            }
            Promise.race = function(promises){
                return new Promise((resolve,reject)=>{
                    promises.forEach( p =>{
                        Promise.resolve(p).then(
                            value =>{
                                resolve(value)
                            },
                            reason =>{
                                reject(reason)
                            }
                        )
                    })
                })
            }
            window.Promise = Promise
})(window)